﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_Crimes : System.Web.UI.UserControl
    {


     
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }



        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

  
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                BindRadio();
                this.ViewState.Add("IsUserControlPostBack", true);

            }



            if (DataItem != null)
            {
                //   cbGender.ListGenderComboBox();

     
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    BindRadio2();
                    lblPenSeq.Text   = DataBinder.Eval(DataItem, "cr_seq").ToString();

                    radStatus.SelectedValue = DataBinder.Eval(DataItem, "record_status").ToString();

                    txtName.Text = DataBinder.Eval(DataItem, "cr_name").ToString(); 
        
             
                }


             

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }


       
      
        }


        private void BindRadio()
        {
            CrimesBLL obj = new CrimesBLL();
            DataTable dt;
            dt = obj.SelectStatus();

            if (dt != null && dt.Rows.Count > 0)
            {
                radStatus.Items.Clear();
                radStatus.DataSource = dt;
                radStatus.DataTextField = "record_status_name";
                radStatus.DataValueField = "record_status";
                //foreach (DataRow cur in dt.Rows)
                //{
                //    ListItem Li = new ListItem();
                //    Li.Text = cur["record_status_name"].ToString();// + " " + ((DateTime)cur["act_start_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH")) + " ถึง " + ((DateTime)cur["act_end_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                //    Li.Value = cur["record_status"].ToString();
                //    radStatus.Items.Add(Li);
                //}

                radStatus.DataBind();
                radStatus.SelectedIndex = 1;
            }
            else
            {
                radStatus.Items.Clear();
            }




        }

        private void BindRadio2()
        {
            CrimesBLL obj = new CrimesBLL();
            DataTable dt;
            dt = obj.SelectStatus();

            if (dt != null && dt.Rows.Count > 0)
            {
                radStatus.Items.Clear();
                radStatus.DataSource = dt;
                radStatus.DataTextField = "record_status_name";
                radStatus.DataValueField = "record_status";
                //foreach (DataRow cur in dt.Rows)
                //{
                //    ListItem Li = new ListItem();
                //    Li.Text = cur["record_status_name"].ToString();// + " " + ((DateTime)cur["act_start_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH")) + " ถึง " + ((DateTime)cur["act_end_date"]).ToString("dd/MM/yyyy", new CultureInfo("th-TH"));
                //    Li.Value = cur["record_status"].ToString();
                //    radStatus.Items.Add(Li);
                //}

                radStatus.DataBind();
      
            }
            else
            {
                radStatus.Items.Clear();
            }




        }










        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            CrimesBLL obj = new CrimesBLL();
 string status,cr_name;

 status = radStatus.SelectedValue;
 cr_name = txtName.Text;
 obj.insert(cr_name, status, 1);   
         
 
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {

            CrimesBLL obj = new CrimesBLL();
            string status, cr_name;
            int cr_seq;
            cr_seq = int.Parse(lblPenSeq.Text);  
            status = radStatus.SelectedValue;
            cr_name = txtName.Text;
            obj.Update(cr_seq,cr_name, status, 1);   



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}